package com.sky.financelake.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.bean.dto.CommunityDto;
import com.sky.financelake.bean.dto.CommunityDto1;
import com.sky.financelake.databinding.ItemCommonFooterBinding;
import com.sky.financelake.databinding.ItemCommunitySkillBinding;
import com.sky.financelake.glide.GlideImgManager;

import java.util.List;

public class CommunitySkillAdapter extends BaseAdapter<CommunityDto1> {

    private Context mContext;

    public CommunitySkillAdapter(Context mContext, List<CommunityDto1> communityDtos) {
        this.mContext = mContext;
        this.datas = communityDtos;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            ItemCommunitySkillBinding binding = ItemCommunitySkillBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new Holder(binding);

        } else if (viewType == TYPE_FOOTER) {
            ItemCommonFooterBinding binding = ItemCommonFooterBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new FootViewHolder(binding);
        }
        return null;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder h, int position) {

        if (h instanceof Holder) {
            Holder holder = (Holder) h;
            holder.tvTitle.setText(datas.get(position).title);
            holder.tvTag.setText(datas.get(position).tag);
            holder.tvAuthor.setText(datas.get(position).username);
            GlideImgManager.glideLoader(mContext, datas.get(position).cover, holder.ivCover, GlideImgManager.Type.Radius);
        } else {

            FootViewHolder footViewHolder = (FootViewHolder) h;
            switch (loadStatus) {
                case LOADING: // 正在加载
                    loadAd.start();
                    footViewHolder.loading.setVisibility(View.VISIBLE);
                    footViewHolder.loadEnd.setVisibility(View.GONE);
                    break;
                case LOADING_COMPLETE: // 加载完成
                    loadAd.stop();
                    footViewHolder.loading.setVisibility(View.GONE);
                    break;
                case LOADING_END: // 加载到底
                    footViewHolder.loading.setVisibility(View.GONE);
                    footViewHolder.loadEnd.setVisibility(View.VISIBLE);
                    break;

                default:
                    break;
            }
        }

    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

    /**
     * 设置上拉加载状态
     *
     * @param loadState 0.正在加载 1.加载完成 2.加载到底
     */
    public void setStatus(int status) {
//        Log.i(TAG, "setLoadState: ");
//        if (loadAd != null && loadState == LOADING) {
//            loadAd.start();
//        }
        this.loadStatus = status;
        notifyDataSetChanged();
    }

    public Integer getStatus(){
        return loadStatus;
    }


    public void clearData() {
        datas.clear();
    }


    private final class Holder extends RecyclerView.ViewHolder {
        ConstraintLayout container;
        AppCompatTextView tvTitle, tvAuthor, tvTag;
        AppCompatImageView ivCover;

        private Holder(final @NonNull ItemCommunitySkillBinding binding) {
            super(binding.getRoot());
            container = binding.container;
            tvTitle = binding.tvTitle;
            tvAuthor = binding.tvAuthor;
            tvTag = binding.tvTag;
            ivCover = binding.ivCover;
            container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        Integer position = getAbsoluteAdapterPosition();
                        mOnItemClickListener.onClick(position, datas.get(position));
                    }
                }
            });
        }
    }

}